package cn.yunyichina.provider.pay.service.impl;

import cn.yunyichina.provider.iface.entity.pay.PayCode;
import cn.yunyichina.provider.iface.entity.pay.PaymentJournalResponseOut;
import cn.yunyichina.provider.pay.constant.OrderType;
import cn.yunyichina.provider.pay.constant.PayStatus;
import cn.yunyichina.provider.pay.dao.PaymentJournalDao;
import cn.yunyichina.provider.pay.entity.PaymentJournal;
import cn.yunyichina.provider.pay.service.PaymentJournalService;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Lullaby on 2017/3/29.
 */
@Service
public class PaymentJournalServiceImpl implements PaymentJournalService {


    private static final Logger logger = LoggerFactory.getLogger(PaymentJournalServiceImpl.class);
    @Autowired
    private PaymentJournalDao paymentJournalDao;

    public PaymentJournal getPaymentJournalByOrderNoAndPaymentDealIdAndPayStatus(String orderNo, String paymentDealId, short payStatus) {
        PaymentJournal paymentJournal = new PaymentJournal();
        paymentJournal.setOrderNo(orderNo);
        paymentJournal.setPaymentDealId(paymentDealId);
        paymentJournal.setPayStatus(payStatus);

        List<PaymentJournal> paymentJournalList = paymentJournalDao.select(paymentJournal);

        if (CollectionUtils.isEmpty(paymentJournalList)) return null;

        return paymentJournalList.get(0);
    }

    public PaymentJournal getPaymentJournal(PaymentJournal paymentJournal) {
        List<PaymentJournal> paymentJournalList = paymentJournalDao.select(paymentJournal);

        if (CollectionUtils.isEmpty(paymentJournalList)) return null;

        return paymentJournalList.get(0);
    }

    /**
     * 更新支付流水是否自动退费的接口
     */
	@Override
	public void updatePaymentJournalRefundFlagByOrderNo(String orderNo,
			short isRefundFlag) {
		 PaymentJournal updatePaymentJournal = new PaymentJournal();
		 updatePaymentJournal.setOrderNo(orderNo);
		 updatePaymentJournal.setIsAutoRefund(isRefundFlag);
		 paymentJournalDao.updatePaymentJournalRefundFlagByOrderNo(updatePaymentJournal);
	}

	public List<PaymentJournalResponseOut> paymentJournalQuery(Map<String,Object> paymentJournalMap){
        logger.info("PaymentJournalServiceImpl.PaymentJournalQuery.paymentJournalMap resquest -> {}", JsonUtils.toJsonString(paymentJournalMap));
        List<Map<String,Object>> paymentJournalList=paymentJournalDao.paymentJournalQuery(paymentJournalMap);
        logger.info("PaymentJournalServiceImpl.PaymentJournalQuery.paymentJournalMap response -> {}", JsonUtils.toJsonString(paymentJournalList));
        List<PaymentJournalResponseOut> paymentJournalResponseOutList=new ArrayList<>();
        if(!CollectionUtils.isEmpty(paymentJournalList)) {
            for (Map paymentMap : paymentJournalList) {
                PaymentJournalResponseOut paymentJournalResponseOut = new PaymentJournalResponseOut();
                paymentJournalResponseOut.setTradeTime(DateUtils.dateToString(DateUtils.StringToDate(paymentMap.get("PAY_TIME").toString())));
                if (OrderType.REGISTER.getCode()== (int)paymentMap.get("ORDER_TYPE") || OrderType.TAKENO.getCode()==(int)paymentMap.get("ORDER_TYPE")) {
                    paymentJournalResponseOut.setOrderMode(Short.valueOf("1"));
                } else if (OrderType.CLINIC.getCode()==(int)paymentMap.get("ORDER_TYPE")) {
                    paymentJournalResponseOut.setOrderMode(Short.valueOf("2"));
                } else if (OrderType.BED.getCode()==(int)paymentMap.get("ORDER_TYPE")) {
                    paymentJournalResponseOut.setOrderMode(Short.valueOf("3"));
                }
                if ("WECHAT".equals(checkPayMode(paymentMap.get("PAY_MODE")))) {
                    paymentJournalResponseOut.setTradeMode(Short.valueOf("3"));
                    paymentJournalResponseOut.setTradePlatform(Short.valueOf("3"));
                } else if ("ALIPAY".equals(checkPayMode(paymentMap.get("PAY_MODE")))) {
                    paymentJournalResponseOut.setTradeMode(Short.valueOf("5"));
                    paymentJournalResponseOut.setTradePlatform(Short.valueOf("5"));
                }
                paymentJournalResponseOut.setPatientName(paymentMap.get("USER_NAME").toString());
                paymentJournalResponseOut.setScheduleDate(paymentMap.get("SCHEDULE_TIME") == null ? null : DateUtils.dateToString(DateUtils.StringToDate(paymentMap.get("SCHEDULE_TIME").toString())));

                if ((int)paymentMap.get("PAY_STATUS")==PayStatus.NORMAL.getCode() || (int)paymentMap.get("PAY_STATUS")==PayStatus.NORMAL_IN.getCode()) {
                    paymentJournalResponseOut.setPsPayOrdNum(paymentMap.get("ORDER_NO").toString());
                    paymentJournalResponseOut.setHisPayOrdNum(paymentMap.get("HIS_ORDER_NO")== null ? null : paymentMap.get("HIS_ORDER_NO").toString());
                    paymentJournalResponseOut.setAgtPayOrdNum(paymentMap.get("PAYMENT_DEAL_ID")== null ? null : paymentMap.get("PAYMENT_DEAL_ID").toString());
                    paymentJournalResponseOut.setPayTotalFee((Integer) paymentMap.get("PAY_AMOUNT"));
                    paymentJournalResponseOut.setTradeType(Short.valueOf("1"));
                }
                if ((int)paymentMap.get("PAY_STATUS")==PayStatus.REFUND.getCode() || (int)paymentMap.get("PAY_STATUS")==PayStatus.REFUND_IN.getCode()) {
                    paymentJournalResponseOut.setPsPayOrdNum(paymentMap.get("ORDER_NO").toString());
                    paymentJournalResponseOut.setHisPayOrdNum(paymentMap.get("HIS_ORDER_NO")== null ? null : paymentMap.get("HIS_ORDER_NO").toString());
                    paymentJournalResponseOut.setAgtPayOrdNum(paymentMap.get("PAYMENT_DEAL_ID")== null ? null : paymentMap.get("PAYMENT_DEAL_ID").toString());
                    paymentJournalResponseOut.setPayTotalFee((Integer) paymentMap.get("PAY_AMOUNT"));
                    paymentJournalResponseOut.setTradeType(Short.valueOf("1"));
                    paymentJournalResponseOut.setPsRefundOrdNum(paymentMap.get("ORDER_NO").toString());
                    paymentJournalResponseOut.setHisRefundOrdNum(paymentMap.get("HIS_ORDER_NO")== null ? null : paymentMap.get("HIS_ORDER_NO").toString());
                    paymentJournalResponseOut.setAgtRefundOrdNum(paymentMap.get("PAYMENT_DEAL_ID")== null ? null : paymentMap.get("PAYMENT_DEAL_ID").toString());
                    paymentJournalResponseOut.setRefundTotalFee((Integer) paymentMap.get("TOTAL_AMOUNT"));
                    paymentJournalResponseOut.setTradeType(Short.valueOf("2"));
                }
                paymentJournalResponseOut.setCollectors(paymentMap.get("COLLECTORS") == null ? null : paymentMap.get("COLLECTORS").toString());
                paymentJournalResponseOut.setInsuranceFee((Integer) paymentMap.get("INSURANCE_AMOUNT"));
                paymentJournalResponseOut.setIsInsurance((Integer)paymentMap.get("IS_INSURANCE"));
                paymentJournalResponseOut.setTotalFee((Integer) paymentMap.get("TOTAL_AMOUNT"));
                paymentJournalResponseOutList.add(paymentJournalResponseOut);
            }
        }
        return  paymentJournalResponseOutList;
    }

    private String  checkPayMode(Object object) {
        if(PayCode.WECHAT.getMode()==(int)(object)|| PayCode.WXAPP.getMode()==(int)(object)|| PayCode.WXJSAPI.getMode()==(int)(object)
                || PayCode.WXNATIVE.getMode()==(int)(object)|| PayCode.WXMICROPAY.getMode()==(int)(object)){
            return "WECHAT";
        }else if(PayCode.ALIPAY.getMode()==(int)(object)|| PayCode.ALIPAYAPP.getMode()==(int)(object)|| PayCode.ALIPAYWAP.getMode()==(int)(object)
                || PayCode.ALIPAYPREPAY.getMode()==(int)(object)|| PayCode.ALIPAYBARPAY.getMode()==(int)(object)
                || PayCode.ALIPAYWAVEPAY.getMode()==(int)(object)|| PayCode.ALIPAYMEDICARE.getMode()==(int)(object)){
            return "ALIPAY";
        }
        return "";
    }

}
